home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Objects / Frames / mm_frameset.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  7.5 KB  |  231 lines

  1. //-- Copyright 1999 Macromedia, Inc. All rights reserved. --
  2.  
  3.  
  4. // Overview:
  5. //  Framesets saved as the HTML for the selected object.
  6. //  Depending upon the current selection a new frameset is created with
  7. //    a single call into the new Dreamweaver splitframe command.
  8. //  
  9.  
  10.  
  11. //---------------     API FUNCTIONS    ---------------
  12.  
  13.  
  14. function objectTag() {
  15.   // Return the html tag that should be inserted
  16.  
  17.   var curDOM = dreamweaver.getDocumentDOM('document');
  18.   var pDOM = dreamweaver.getDocumentDOM('parent');
  19.   
  20.   // Check to see if current document is a frameset.
  21.   if (curDOM.body.tagName.toUpperCase() == 'FRAMESET') {
  22.       var curOffsets = curDOM.getSelection();
  23.       var curNode = curDOM.offsetsToNode(curOffsets[0], curOffsets[1]);
  24.     // Wrap current selection with new Frameset.
  25.     wrapFrame(curDOM, curNode, getFrameSpec());    
  26.   // Otherwise, check if there is a parent frameset.
  27.   } else  if (pDOM == null) {
  28.     // No parent, create new frameset and wrap current document
  29.     newFrame(curDOM, getFrameSpec());
  30.   } else {   
  31.     // Wrap current Frameset with new Frameset.
  32.     if (!pDOM) {return;} // Check for invalid DOM.
  33.     wrapFrame(pDOM, activeFrameNode(), getFrameSpec());
  34.   }
  35.   
  36.   // Finally return nothing. No values actually inserted.
  37. }
  38.  
  39.  
  40. //---------------    LOCAL FUNCTIONS   ---------------
  41.  
  42.  
  43. // Function: wrapFrame
  44. // Description: Wraps the passed template Frameset DOM around the current
  45. //  selection (this may either be a document or frameset).
  46. // Arguments: 
  47. //   curDOM - the current DOM
  48. //   curNode - the currently "selected" node in the current DOM
  49. //   newFSDOM - DOM for new Frameset template (one frame should be named mm_target).
  50. function wrapFrame (curDOM, curNode, tfsDOM) {
  51.   var newFrameStr = '';
  52.   var targetSelection = curDOM.nodeToOffsets(curNode);
  53.   var cArr, tArr;
  54.   
  55.   // Target frame specifies the content for the new frame.
  56.   if (!tfsDOM.mm_target) {return;} // Check for no target defined.
  57.   
  58.   // Modify the current frameset to include new frames.
  59.   newFrameStr += curDOM.documentElement.outerHTML.substring(0,targetSelection[0]);
  60.  
  61.   // Insert the curNode into the mm_target frame of the new frameset DOM.
  62.   var curNodeHTM = curNode.outerHTML;
  63.   var tfsTargetOffsets = tfsDOM.nodeToOffsets(tfsDOM.mm_target);
  64.   var tfsHTM = tfsDOM.documentElement.outerHTML;
  65.   var tfsBodyOffsets = tfsDOM.nodeToOffsets(tfsDOM.body);
  66.  
  67.   newFrameStr += tfsHTM.substring(tfsBodyOffsets[0],tfsTargetOffsets[0]) +
  68.                  curNodeHTM +
  69.                  tfsHTM.substring(tfsTargetOffsets[1],tfsBodyOffsets[1]);
  70.  
  71.   // Add the remaining new frame.
  72.   newFrameStr += curDOM.documentElement.outerHTML.substring(targetSelection[1]);
  73.   
  74.   // Set the current document.
  75.   curDOM.documentElement.outerHTML = newFrameStr;
  76. }
  77.  
  78.  
  79. // Function: childNodeNum
  80. // Description: Determine the number of the given childNode.
  81. function childNodeNum(cNode) {
  82.   var curNum = 0;
  83.   var rtnNum = null;
  84.   
  85.   if (cNode.parentNode) {
  86.     var numChildren = cNode.parentNode.childNodes.length;
  87.     for (curNum = 0; curNum < numChildren; curNum++) {
  88.       if (cNode.parentNode.childNodes[curNum] == cNode) {
  89.         rtnNum = curNum;
  90.         break;
  91.   } } }
  92.   return rtnNum;
  93. }
  94.  
  95. // Function: getListItem
  96. // Description: Returns numbered item in list.
  97. function getListItem(inList, itemNum) {
  98.   var curArr = inList.split(',');
  99.   return curArr[itemNum];
  100. }
  101.  
  102. // Function: sizeRelOr
  103. // Description: Returns true if the child frame node size is relative
  104. //   and the frameset is either a row or col, but not both.
  105. function sizeRelOr(cNode) {
  106.   var fsCols = cNode.parentNode.cols;
  107.   var fsRows = cNode.parentNode.rows;
  108.   var rtnBool = false;
  109.   if ((fsCols != null) && (fsRows != null)) { // Exclusive OR
  110.     rtnBool = false;
  111.   } else {
  112.     if (fsCols) {
  113.       if (getListItem(fsCols, childNodeNum(cNode)) == "*") {
  114.         rtnBool = true;
  115.       }
  116.     } else {
  117.       if (getListItem(fsRows, childNodeNum(cNode)) == "*") {
  118.         rtnBool = true;
  119.   } } }
  120.   return rtnBool;
  121. }
  122.  
  123.  
  124. // Function: newFrame
  125. // Description: Create a new frameset document and place the existing document into new Frameset.
  126. // Arguments:
  127. //  inDOM - 
  128. //  newFSDOM - 
  129. function newFrame (inDOM, newFSDOM) {
  130.   if (!(typeof inDOM.splitFrame == 'function')) {return;} // Function not defined.
  131.   if (!dw.getDocumentDOM().canSplitFrame()) {return;} // Not a valid selection.
  132.  
  133.   var frameStr = '';
  134.  
  135.   // Create a new frameset
  136.   //  Note: Original document refereced below as childNodes[1]
  137.   inDOM.splitFrame('right');
  138.  
  139.   // Get new frameset document DOM (frameset should be the 
  140.   //  active document after split command)
  141.   var curDOM = dreamweaver.getDocumentDOM('document');
  142.   if (!curDOM) {return;} // Check for invalid DOM.
  143.  
  144.   // Target frame specifies the content for the new frame.
  145.   if (newFSDOM) {
  146.     var newSrc = curDOM.body.childNodes[1].src;
  147.     // Replace the entire frameset with the template frameset.
  148.       curDOM.body.outerHTML = newFSDOM.body.outerHTML;
  149.     curDOM.mm_target.src = newSrc;
  150.     curDOM.mm_target.name = 'mainFrame';
  151.  
  152.   } // else if target not found, do nothing.
  153. }
  154.  
  155.  
  156. // Function: activeFrameOffsets
  157. // Description: Return the node into the frameset for the currently
  158. //  selected frame. If no frame is selected return the offset for
  159. //  the entire frameset.
  160. function activeFrameNode () {
  161.   var curDOM = dreamweaver.getDocumentDOM('document');
  162.   var pDOM = dreamweaver.getDocumentDOM('parent');
  163.   var frameNodeList = pDOM.getElementsByTagName('frame');
  164.   var numberOfFrames = frameNodeList.length;
  165.   var returnNode = pDOM.body;
  166.   
  167.   for (var i = 0; i < numberOfFrames; i++) { 
  168.     if (dreamweaver.getDocumentDOM('parent.frames['+i+']') == curDOM) {
  169.       returnNode = frameNodeList[i];
  170.       break;
  171.   } }
  172.   return returnNode;
  173. }
  174.  
  175.  
  176. // Function: cleanFrameset
  177. // Description: Remove any extranious data in the current frameset template and
  178. //  give all the frames a unique name.
  179. // Arguments:
  180. function cleanFrameset(fsDOM) {
  181.   //  Remove existing src attributes.
  182.   if (fsDOM != null) {
  183.     var frameNodeList = fsDOM.getElementsByTagName('frame');
  184.     var numberOfFrames = frameNodeList.length;
  185.     var oldName = 'FrameName'; // Default frame name if none specified.
  186.  
  187.     for (var curFrame = 0; curFrame < numberOfFrames; curFrame++) {
  188.       if (frameNodeList[curFrame].name) // Check that the frame has a name.
  189.           oldName = frameNodeList[curFrame].name;
  190.       if (oldName != "mm_target") { // Set a unique name and cleanup frame tag.
  191.         frameNodeList[curFrame].name = makeUniqueFrameName(oldName);
  192.         // Remove the invalid src attribute.
  193.         frameNodeList[curFrame].removeAttribute("src");
  194.   } } }
  195.  
  196.   return fsDOM;
  197. }
  198.  
  199.  
  200. // Function: getFrameSpec
  201. // Description: Returns the object frame specification.
  202. function getFrameSpec () {
  203.   // The frameset *is* the object's HTML.
  204.   var rtnDOM = cleanFrameset(document);
  205.   // Return frameset specification it.
  206.   return rtnDOM;
  207. }
  208.  
  209.  
  210. // Create a unique frame name.
  211. // For ex: makeUniqueFrameName("leftFrame") returns leftFrame, leftFrame1, leftFrame2, etc.
  212.  
  213. function makeUniqueFrameName(baseName) {
  214.   var objArray,tagCounter=1,i,possName,name,DOM,nameList="";
  215.  
  216.   possName = baseName;
  217.   DOM = dreamweaver.getDocumentDOM("parent");
  218.   if (!DOM) DOM = dreamweaver.getDocumentDOM("document");
  219.   objArray = DOM.body.getElementsByTagName('FRAME');
  220.   if (objArray.length > 0) { //other images, check
  221.     for (i=0; i<objArray.length; i++) { //create list of all img names
  222.       name = objArray[i].getAttribute("name"); if (name) nameList += " " + name + " ";
  223.       name = objArray[i].getAttribute("id"); if (name) nameList += " " + name + " ";
  224.     }
  225.     while (nameList.indexOf(possName) != -1) possName = baseName+tagCounter++; //find 1st avail
  226.   }
  227.   return possName
  228. }
  229.  
  230.  
  231.